﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;

namespace HRM
{
    public partial class frmViewAttendance : Form
    {        
        int _DepartmentID = 0;
        int _BranchID = 0;
        EmployeeShiftClient _wcfEmployeeShift;
        EmployeeShiftInfo _ESInfo;
        MarkWorkClient _wcfMarkWork;
        ShiftClient _wcfShift;
        DateTime _date;
        int _EmployeeID = 0;
        int _ShiftID = 0;

        public frmViewAttendance(int BranchID, int DepartmentID, DateTime dateVal)
        {
            InitializeComponent();
            _BranchID = BranchID;
            _DepartmentID = DepartmentID;
            _date = dateVal;
            _wcfMarkWork = Service.MarkWork();
            _wcfShift = Service.Shift();
            _wcfEmployeeShift = Service.EmployeeShift();
            _ESInfo = new EmployeeShiftInfo();
            LoadData();
        }

      

        private class MyHeader : SourceGrid.Cells.ColumnHeader
        {
            public MyHeader(object value)
                : base(value)
            {
                //1 Header Row
                SourceGrid.Cells.Views.ColumnHeader view = new SourceGrid.Cells.Views.ColumnHeader();
                view.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
                view.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
                View = view;
                AutomaticSortEnabled = false;
            }
        }

        private void LoadData()
        {
            DataTable dtEmployee = _wcfEmployeeShift.EmployeeShift_GetEmployee(_DepartmentID).Tables[0];

            dgvList.Redim(dtEmployee.Rows.Count+2, 22);
            dgvList.FixedRows = 2;
            dgvList[0, 0] = new MyHeader(UpdateUI.GetString("nhanvien")); dgvList[0, 0].RowSpan = 2;

            for (int i = 1; i < dgvList.Columns.Count; i = i + 3)
            {
                dgvList[0, 1] = new MyHeader(UISetting.GetDayOfWeek(_date) + "\n" + _date.ToString("dd/MM/yyyy"));
                dgvList[0, 1].ColumnSpan = 3;

                dgvList[0, 1 + 3] = new MyHeader(UISetting.GetDayOfWeek(_date.AddDays(1)) + "\n" + _date.AddDays(1).ToString("dd/MM/yyyy"));
                dgvList[0, 1 + 3].ColumnSpan = 3;

                dgvList[0, 1 + 2 * 3] = new MyHeader(UISetting.GetDayOfWeek(_date.AddDays(2)) + "\n" + _date.AddDays(2).ToString("dd/MM/yyyy"));
                dgvList[0, 1 + 2 * 3].ColumnSpan = 3;

                dgvList[0, 1 + 3 * 3] = new MyHeader(UISetting.GetDayOfWeek(_date.AddDays(3)) + "\n" + _date.AddDays(3).ToString("dd/MM/yyyy"));
                dgvList[0, 1 + 3 * 3].ColumnSpan = 3;

                dgvList[0, 1 + 4 * 3] = new MyHeader(UISetting.GetDayOfWeek(_date.AddDays(4)) + "\n" + _date.AddDays(4).ToString("dd/MM/yyyy"));
                dgvList[0, 1 + 4 * 3].ColumnSpan = 3;

                dgvList[0, 1 + 5 * 3] = new MyHeader(UISetting.GetDayOfWeek(_date.AddDays(5)) + "\n" + _date.AddDays(5).ToString("dd/MM/yyyy"));
                dgvList[0, 1 + 5 * 3].ColumnSpan = 3;

                dgvList[0, 1 + 6 * 3] = new MyHeader(UISetting.GetDayOfWeek(_date.AddDays(6)) + "\n" + _date.AddDays(6).ToString("dd/MM/yyyy"));
                dgvList[0, 1 + 6 * 3].ColumnSpan = 3;

            }

            for (int i = 1; i < dgvList.Columns.Count; i = i + 3)
            {

                dgvList[1, i] = new MyHeader(UpdateUI.GetString("calam"));
                dgvList[1, i + 1] = new MyHeader(UpdateUI.GetString("thoigian"));
                dgvList[1, i + 2] = new MyHeader(UpdateUI.GetString("giolamthem"));
              
            }

            
            //Vẽ datagrid:
            for (int i = 0; i < dtEmployee.Rows.Count; i++)
            {
                dgvList[i + 2, 0] = new SourceGrid.Cells.Cell(dtEmployee.Rows[i]["FullName"].ToString(), typeof(string));
                for (int j = 1; j < dgvList.Columns.Count; j++)
                {
                    dgvList[i + 2, j] = new SourceGrid.Cells.Cell();
                }
            }

            //Load data:

            for (int i = 0; i < dtEmployee.Rows.Count; i++)
            {
                _EmployeeID = Convert.ToInt32(dtEmployee.Rows[i]["Employee_ID"]);

                for (int j = 1; j < dgvList.Columns.Count; j = j + 3)
                {
                    int _temp = (j - 1) / 3;
                    DateTime _currDate = Convert.ToDateTime(_date.AddDays(_temp));
                    _ESInfo = _wcfEmployeeShift.EmployeeShift_GetShift(_EmployeeID, _currDate);
                    DataTable dtMarkWork = _wcfMarkWork.MarkWork_GetEmployeeInfoByDate(_EmployeeID, _currDate).Tables[0];
                    if (_ESInfo != null)
                    {
                        if (dtMarkWork.Rows.Count > 0)
                        {
                            _ShiftID = Convert.ToInt32(dtMarkWork.Rows[0]["MarkWork_ShiftID"]);
                            if (_wcfShift.Shift_GetInfo(_ShiftID).TimeInB.Trim().Length == 0)
                            {
                                dgvList[i + 2, j].Value = _wcfShift.Shift_GetInfo(_ShiftID).Name;
                                dgvList[i + 2, j + 1].Value = Convert.ToDateTime(dtMarkWork.Rows[0]["MarkWork_TimeIn"]).ToString("HH:mm")
                                    + " - " + Convert.ToDateTime(dtMarkWork.Rows[0]["MarkWork_TimeOut"]).ToString("HH:mm");
                                dgvList[i + 2, j + 2].Value = Convert.ToDecimal(dtMarkWork.Rows[0]["MarkWork_OverTime"]).ToString(UIParamater.Cham2So);
                            }
                            else
                            {
                                dgvList[i + 2, j].Value = _wcfShift.Shift_GetInfo(_ShiftID).Name;
                                dgvList[i + 2, j + 1].Value = Convert.ToDateTime(dtMarkWork.Rows[0]["MarkWork_TimeIn"]).ToString("HH:mm")
                                    + " - " + Convert.ToDateTime(dtMarkWork.Rows[0]["MarkWork_TimeOut"]).ToString("HH:mm")
                                    + " - " + Convert.ToDateTime(dtMarkWork.Rows[0]["MarkWork_TimeInB"]).ToString("HH:mm")
                                    + " - " + Convert.ToDateTime(dtMarkWork.Rows[0]["MarkWork_TimeOutB"]).ToString("HH:mm");
                                dgvList[i + 2, j + 2].Value = Convert.ToDecimal(dtMarkWork.Rows[0]["MarkWork_OverTime"]).ToString(UIParamater.Cham2So);
                            }
                        }
                    }
                }
            }
            dgvList.AutoStretchColumnsToFitWidth = true;
            dgvList.AutoSizeCells();
        }

        private void frmViewAttendance_FormClosing(object sender, FormClosingEventArgs e)
        {
            _wcfEmployeeShift.Close();
            _wcfMarkWork.Close();
            _wcfShift.Close();
        }
    }
}
